//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "TypePayoff.h"
using namespace Cephei::QL::Instruments;
#include <gen/QL/Payoff.h>
using namespace Cephei::QL;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Instruments::CTypePayoff::CTypePayoff (boost::shared_ptr<QuantLib::TypePayoff>& childNative, Object^ owner) : CPayoff(CTypePayoff::typeid)
{
#ifdef HANDLE
	_phTypePayoff = NULL;
#endif
	_ppTypePayoff = &childNative;
    _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppTypePayoff));
}
Cephei::QL::Instruments::CTypePayoff::CTypePayoff (QuantLib::TypePayoff& childNative, Object^ owner) : CPayoff(CTypePayoff::typeid)
{
#ifdef HANDLE
	_phTypePayoff = NULL;
#endif
	_ppTypePayoff = new boost::shared_ptr<QuantLib::TypePayoff> (&childNative);
    _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppTypePayoff));
    _TypePayoffOwner = owner;
    _PayoffOwner = owner;
}

Cephei::QL::Instruments::CTypePayoff::CTypePayoff (CTypePayoff^ copy) : CPayoff(CTypePayoff::typeid)
{
#ifdef HANDLE
	_phTypePayoff = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTypePayoff = new boost::shared_ptr<QuantLib::TypePayoff> (copy->GetShared());
        _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppTypePayoff));
    }
}
Cephei::QL::Instruments::CTypePayoff::CTypePayoff (PLATFORM::Type^ t) : CPayoff(CTypePayoff::typeid)
{
#ifdef HANDLE
	_phTypePayoff = NULL;
#endif
	if (!t->IsSubclassOf(CTypePayoff::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Instruments::CTypePayoff::CTypePayoff (QuantLib::Handle<QuantLib::TypePayoff>& childNative, Object^ owner)  : CPayoff(CTypePayoff::typeid)
{
	_phTypePayoff = &childNative;
	_ppTypePayoff = &static_cast<boost::shared_ptr<QuantLib::TypePayoff>>(childNative.currentLink());
    _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppTypePayoff));
    _TypePayoffOwner = owner;
}
Cephei::QL::Instruments::CTypePayoff::CTypePayoff (QuantLib::Handle<QuantLib::TypePayoff> childNative)  : CPayoff(CTypePayoff::typeid)
{
	_phTypePayoff = &childNative;
	_ppTypePayoff = &static_cast<boost::shared_ptr<QuantLib::TypePayoff>>(childNative.currentLink());
    _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppTypePayoff));
}
#endif
#ifdef STRUCT
Cephei::QL::Instruments::CTypePayoff::CTypePayoff (QuantLib::TypePayoff childNative)  : CPayoff(CTypePayoff::typeid)
{
#ifdef HANDLE
	_phTypePayoff = NULL;
#endif
	_ppTypePayoff = new boost::shared_ptr<QuantLib::TypePayoff> (new QuantLib::TypePayoff (childNative));
    _ppPayoff = new boost::shared_ptr<QuantLib::Payoff> (boost::dynamic_pointer_cast<QuantLib::Payoff> (*_ppTypePayoff));
}
#endif

Cephei::QL::Instruments::CTypePayoff::~CTypePayoff ()
{
    if (_ppTypePayoff != NULL)
    {
	    delete _ppTypePayoff;
        _ppTypePayoff = NULL;
    }
}
Cephei::QL::Instruments::CTypePayoff::!CTypePayoff ()
{
    if (_ppTypePayoff != NULL)
    {
	    delete _ppTypePayoff;
    }
}
QuantLib::TypePayoff& Cephei::QL::Instruments::CTypePayoff::GetReference ()
{
    if (_ppTypePayoff == NULL) throw REFNEW NativeNullException ();
	return **_ppTypePayoff;
}
boost::shared_ptr<QuantLib::TypePayoff>& Cephei::QL::Instruments::CTypePayoff::GetShared ()
{
    if (_ppTypePayoff == NULL) throw REFNEW NativeNullException ();
	return *_ppTypePayoff;
}
QuantLib::TypePayoff* Cephei::QL::Instruments::CTypePayoff::GetPointer ()
{
    if (_ppTypePayoff == NULL) throw REFNEW NativeNullException ();
	return &**_ppTypePayoff;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TypePayoff>& Cephei::QL::Instruments::CTypePayoff::GetHandle ()
{
	if (_phTypePayoff == NULL)
	{
		_phTypePayoff = new Handle<QuantLib::TypePayoff> (*_ppTypePayoff);
	}
	return *_phTypePayoff;
}
#endif
bool Cephei::QL::Instruments::CTypePayoff::HasNative () 
{
	return (_ppTypePayoff != NULL);
}

String^ Cephei::QL::Instruments::CTypePayoff::Description::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppTypePayoff)->description ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Option::TypeEnum Cephei::QL::Instruments::CTypePayoff::OptionType::get ()
{
    try
    {
    	QuantLib::Option::Type _rv = (QuantLib::Option::Type)(*_ppTypePayoff)->optionType ( );   
        QL::Option::TypeEnum _nrv = (QL::Option::TypeEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

